package cn.yaboya._02_sync;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;

/**
 * @BelongsProject: rocketmq-test
 * @BelongsPackage: cn.itsource._02_sync
 * @Author: LT
 * @CreateTime: 2022-7-23 20:53:15
 * @Description: 发送异步消息
 * @Version: 1.0
 */
public class Producer {

    public static void main(String[] args) throws Exception {
        // 1.创建消费者对象
        DefaultMQProducer producer = new DefaultMQProducer("hello_producer_group");
        // 2.连接mq
        producer.setNamesrvAddr("127.0.0.1:9876");
        // 3.启动消费者
        producer.start();
        // 4.发送异步消息
        Message message = new Message("hello_topic2", "hello_tags2", "我在源码学Java！".getBytes(StandardCharsets.UTF_8));
        producer.send(message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println(sendResult);
            }

            @Override
            public void onException(Throwable throwable) {
                throwable.printStackTrace();
                System.out.println("发送消息失败");
            }
        });
    }

}